const app = getApp();
Page({
  data: {
    StatusBar: app.globalData.StatusBar,
    CustomBar: app.globalData.CustomBar,
    isShow: true,
    isNone: false,
    writelist: '',
    title: '',
    page: 1,
    headpic:''
    // userImg:null
  },

  onLoad: function () {
    this.getidCard();
    let open_id = wx.getStorageSync('user_new')
    app.core('get', 'userInfo?open_id=' + open_id, '', (res) => {
      console.log(res)
      this.setData({
        headpic: res.data.headpic
      })
    })
  },

  //获取列表
  getidCard: function () {
    var vm = this;
    var page = vm.data.page;
    var openid = wx.getStorageSync('user_new')
    app.core('get', 'get_article_by_id' + '/page/' + page + '/open_id/' + openid, '', (res) => {
      console.log(res)
      if (res.data) {
        vm.setData({
          writelist: res.data,
          // userImg: app.globalData.userInfo.avatarUrl
        })
      } else {
        vm.setData({
          isNone: true,
          isShow: false
        })
      }
    },
      (err) => {
        console.log(err)
      }
    )
  },
  getidCardmore(page) {
    wx.showLoading({
      title: '正在加载',
    })
    var vm = this;
    var page = vm.data.page;
    var openid = wx.getStorageSync('user_new')
    app.core('get', 'get_article_by_id' + '/page/' + page + '/open_id/' + openid, '', (res => {
      console.log(res.data)
      vm.setData({
        // newslist: res.data
        // 后台接口请求的数据不是在原有基础上继续请求而是直接凭借
        writelist: vm.data.writelist.concat(res.data),
        page: page + 1,
      })
    }))
    setTimeout(() => { wx.hideLoading() }, 500)
  },
  //删除
  removeItem(e) {
    console.log(e.target.dataset.id)
    var vm = this;
    wx.showModal({
      title: '系统提示',
      content: '确定要删除当前文章吗',
      success(res) {
        if (res.confirm) {
          //删除
          var id = e.target.dataset.id
          var URL = "del_article_by_id" + '/id/' + id;
          app.core('get', URL, {
          }, (res) => {
            console.log(res)
            setTimeout(function () {
              vm.onLoad();
            }, 500);
          },
            (err) => {
              console.log(err)
            }
          )
        } else {
          console.log('用户点击取消')
        }
      }
    })
  },

  //查看名片详情
  descItem(e) {
    console.log(e);
    var Url = e.currentTarget.dataset.url;
    wx.navigateTo({
      url: Url,
    })
  },

  //搜索名片
  searchlist: function (e) {
    var title = (e.detail.value).replace(/\s+/g, '');
    if (title == '') {
      this.setData({
        isShow: true,
        isNone: false,
      });
    } else {
      this.setData({
        title: title
      })
    }
  },

  searchdesc: function () {
    var vm = this;
    var page = vm.data.page;
    var openid = wx.getStorageSync('user_new')
    var keys = (vm.data.title).replace(/\s+/g, '');
    if (keys == '') {
      wx.showToast({
        title: '请输入关键字',
        duration: 2000,
        icon: 'none'
      })
    } else {
      app.core('get', 'get_article_by_id' + '/page/' + page + '/open_id/' + openid + '?title=' + keys, '', (res => {
        console.log(res)
        if (res.data.length === 0) {
          vm.setData({
            isNone: true,
            isShow: false
          })
        } else {
          vm.setData({
            writelist: res.data
          })
        }
      })
      )
    }
    // app.core('get', 'get_article_by_id'+'/page/'+page+'/open_id/'+ openid +'?title='+keys,(res) => {
    //   console.log(res)
    // })
    // }
  },
  onReachBottom: function () {
    var that = this;
    var page = that.data.page;
    console.log(page);
    that.getidCardmore(page)
  },

})